package org.hardcode.rpc.test.consumer.codec;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.hardcode.rpc.test.consumer.codec.init.RpcTestConsumerInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 模拟消费者启动
 * 启动消费者并连接到服务提供者
 * 还会触发 RpcTestConsumerHandler   channelActive()方法 向服务提供者发送数据
 * 并在RpcTestConsumerHandler 中 channelRead0（） 接收服务提供者响应的数据
 */
public class RpcTestConsumer {

    private static Logger logger = LoggerFactory.getLogger(RpcTestConsumer.class);
    public static void main(String[] args) throws InterruptedException {
        Bootstrap  bootstrap = new Bootstrap();

        EventLoopGroup  eventLoopGroup =  new NioEventLoopGroup(4);
        try {
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new RpcTestConsumerInitializer());

            bootstrap.connect("127.0.0.1",27883).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            logger.info("睡眠开始");
            Thread.sleep(2000);
            logger.info("睡眠结束");
            eventLoopGroup.shutdownGracefully();
            logger.info("关闭");
        }

    }
}
